package tech.wetech.admin3.controller;

import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import tech.wetech.admin3.sys.model.Message;
import tech.wetech.admin3.sys.service.MsgCenterService;
import tech.wetech.admin3.util.CustomResponse;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/msgCenter")
public class MsgCenterController {
  @Autowired
  private MsgCenterService msgCenterService;

  @GetMapping("/messages")
  public List<Message> getMessagesForUser(@RequestParam String email) {
    return msgCenterService.getMessagesForUser(email);
  }

  @GetMapping("/unreadmessages")
  public List<Message> getUnreadMessagesForUser(@RequestParam String email) {
    return msgCenterService.getUnreadMessagesForUser(email);
  }

  @GetMapping("/search")
  public List<Message> searchMessages(@RequestParam String email, @RequestParam String digitalName) {
    return msgCenterService.searchMessages(email, digitalName);
  }

  @PutMapping("/messages/{id}")
  public ResponseEntity<?> markMessageAsRead(@PathVariable Integer id) {
    msgCenterService.markMessageAsRead(id);
    return ResponseEntity.ok(new CustomResponse.r1<>("标记已读", true, null));
  }

  @DeleteMapping("/readmessages")
  public ResponseEntity<?> deleteReadMessages(@RequestParam String email) {
    msgCenterService.deleteReadMessages(email);
    return ResponseEntity.ok(new CustomResponse.r1<>("删除成功", true, null));
  }

  @DeleteMapping("/messages/{ids}")
  public ResponseEntity<?> deleteSpecificMessages(@PathVariable List<Integer> ids) {
    msgCenterService.deleteSpecificMessages(ids);
    return ResponseEntity.ok(new CustomResponse.r1<>("删除成功", true, null));
  }
}
